DIP Assignment¶

Image processing using openCV and Python¶

  1. Write a program to do image format conversion i.e. from
    • RGB -> Gray
    • Gray -> Binary
    • RGB -> Binary
    • RGB -> HSV
    • HSV -> RGB
    • RGB -> YCbCr
    • YCbCr -> RGB
In [ ]:
import cv as cv
import numpy as np
from matplotlib import pyplot as plt
%matplotlib inline

# read an image using openCV
img = cv.imread("./img/flowers.jpg")

# converting the images in different formats

rgb_img = cv.cvtColor(img, cv.COLOR_BGR2RGB)
gray_img = cv.cvtColor(rgb_img, cv.COLOR_RGB2GRAY)

# rgb -> gray
rgb_to_gray = gray_img

# gray -> binary
_, gray_to_bin = cv.threshold(gray_img, 127, 255, cv.THRESH_BINARY)

# rgb -> binary 
_, rgb_to_bin = cv.threshold(gray_img, 175, 255, cv.THRESH_BINARY)

# rgb -> hsv
rgb_to_hsv = cv.cvtColor(rgb_img, cv.COLOR_RGB2HSV)

# hsv -> rgb
hsv_img = cv.cvtColor(rgb_img, cv.COLOR_RGB2HSV)
hsv_to_rgb = cv.cvtColor(hsv_img, cv.COLOR_HSV2RGB)

# rgb -> ycbcr
rgb_to_ycbcr = cv.cvtColor(rgb_img, cv.COLOR_RGB2YCR_CB)

# ycrcb -> rgb
ycrcb_img = cv.cvtColor(rgb_img, cv.COLOR_RGB2YCrCb)
ycrcb_to_rgb = cv.cvtColor(ycrcb_img, cv.COLOR_YCrCb2RGB)


# display an image
# plt.imshow(img)
# plt.show()

# cv.imshow("Original", img)
# cv.imshow("Grayscale", rgb_to_gray)
# cv.imshow("Black n White", gray_to_bin)
# cv.imshow("RGB to Binary", rgb_to_bin)
# cv.imshow("RGB to HSV", rgb_to_hsv)
# cv.imshow("HSV to RGB", hsv_to_rgb)
# cv.imshow("RGB to YCbCr", rgb_to_ycbcr)
# cv.imshow("YCbCr to RGB", ycrcb_to_rgb)
# cv.waitKey(0)
# cv.destroyAllWindows()

 
  1. Write a program to read an image and rotate that image in clockwise and anticlockwise direction and display it
In [3]:
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt

img = cv.imread('./img/human-crop.jpg')
img = cv.cvtColor(img, cv.COLOR_BGR2RGB)
rows, cols, _ = img.shape

# Create transformation matirx
# M1 for anti clockwise and M2 for clockwise rotation
M1 = cv.getRotationMatrix2D(((cols-1)/2.0, (rows-1)/2.0), 90, 1)
M2 = cv.getRotationMatrix2D(((cols-1)/2.0, (rows-1)/2.0), -90, 1)

# rotating image using the matrix generates (M) anticlockwise 
img_anticlockwise = cv.warpAffine(img, M1, (cols, rows))
# rotating in clockwise direction
img_clockwise = cv.warpAffine(img, M2, (cols, rows))

# display an image
plt.figure(figsize=(8,8))
plt.subplot(2,2,1)
plt.imshow(img)
plt.title("Original Image", fontweight='bold')
# plt.axis("off")

plt.subplot(2,2,3)
plt.imshow(img_clockwise)
plt.title("Clockwise Rotated", fontweight="bold")
plt.axis("off")

plt.subplot(2,2,4)
plt.imshow(img_anticlockwise)
plt.title("Anti Clockwise Rotated", fontweight="bold")
plt.axis("off")
plt.show()
No description has been provided for this image
  1. Write a program to find the histogram of a gray image and display the histogram
In [5]:
import cv2 as cv
import matplotlib.pyplot as plt

# read the image
img = cv.imread("./img/hill.png")
rgb_img = cv.cvtColor(img, cv.COLOR_BGR2RGB)
gray_img = cv.cvtColor(cv.cvtColor(img, cv.COLOR_BGR2GRAY), cv.COLOR_BGR2RGB)


# calculate histogram 
hist = cv.calcHist([gray_img], [0], None, [256], [0, 256])

# display histogram
plt.figure(figsize=(8,8))
plt.subplot(2,2,1)
plt.imshow(rgb_img)
plt.title("Original Image", fontweight='bold')
plt.axis("off")

plt.subplot(2,2,2)
plt.imshow(gray_img)
plt.title("Gray Image", fontweight="bold")
plt.axis("off")

plt.subplot(2,2,(3,4))
plt.plot(hist)
plt.hist(img.flatten(), 256, [0, 256])
plt.title("Histogram of Gray Image", fontweight="bold")
plt.show()
No description has been provided for this image
  1. Write a program to perform color separation into R, G and B from an color image
In [6]:
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt

# read a color image
img = cv.imread("./img/candy.jpg")
rgb_img = cv.cvtColor(img, cv.COLOR_BGR2RGB)
rows, cols, _ = img.shape

# separating color channels
r = img[:,:,2]
g = img[:,:,1]
b = img[:,:,0]

red_channel = cv.cvtColor(r, cv.COLOR_BGR2RGB)
green_channel = cv.cvtColor(g, cv.COLOR_BGR2RGB)
blue_channel = cv.cvtColor(b, cv.COLOR_BGR2RGB)

# display the original image
# and the sperated R, G, B channels 
# of the image
plt.figure(figsize=(8,8))

plt.subplot(2,3,(1,2))
plt.imshow(rgb_img)
plt.title("Original Image", fontweight="bold")
plt.axis("off")

plt.subplot(2,3,4)
plt.imshow(red_channel)
plt.title("Red Channel", fontweight="bold")
plt.axis("off")

plt.subplot(2,3,5)
plt.imshow(green_channel)
plt.title("Green Channel", fontweight="bold")
plt.axis("off")

plt.subplot(2,3,6)
plt.imshow(blue_channel)
plt.title("Blue Channel", fontweight="bold")
plt.axis("off")

plt.show()
No description has been provided for this image
  1. Write a program to enhance the image in spatial domain using
    • Image negative
    • Log transformation
    • Power Law transform
    • Piecewise linear transform
In [8]:
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt


# ============================
# handler functions
# ============================
# image negative
def img_negative(color_img):
    img_neg = cv.bitwise_not(color_img)

    return img_neg


# image log transform
def img_log(color_img):
    
    img_log = (np.log(color_img+1)/(np.log(1+np.max(color_img))))*255
    img_log = np.array(img_log, dtype=np.uint8)

    return img_log


# image power law transform
def img_powerLaw(color_img, gamma):
    # applying Gamma value (gamma) on the normalised image and
    # then multiplying by scaling constant
    img_gamma = np.array(255*(color_img/255)**gamma, dtype='uint8')
    
    return img_gamma


# piecewise linear transform: contrast stretching
def img_contrast_stretch(gray_img):
    rows, cols = gray_img.shape

    minmax_img = np.zeros((rows, cols), dtype='uint8')
    for i in range(rows):
        for j in range(cols):
            minmax_img[i,j] = 255 * (img[i,j]-np.min(img))/(np.max(img)-np.min(img))

    return minmax_img


# piecewise linear transform: intensity level slicing
def img_intensity_slicing(gray_img):
    rows, cols = gray_img.shape
    img1 = np.zeros((rows, cols), dtype=np.uint8)

    # specify min and max range
    min_range = 10
    max_range = 60

    for i in range(rows):
        for j in range(cols):
            if img[i,j] > min_range and img[i,j] < max_range:
                img1[i,j] = 255
            else:
                img1[i,j] = 0

    return img1


# piecewise linear transform: bit plane slicing
def img_bit_slicing(gray_img, nbit):
    rows, cols = gray_img.shape
    
    # changing pixel value of image to binary
    img = []
    for i in range(rows):
        for j in range(cols):
            img.append(np.binary_repr(gray_img[i][j], width=8)) 
    
    # extract the bit planes and
    # multiplying with 2 ^ (nbit - 1) 
    # and reshaping to reconstruct the bit image
    bit_plane = 8 - nbit
    mult_factor = 2 ** (nbit - 1)
    result_img = (np.array([int(i[bit_plane]) for i in img], dtype=np.uint8) * mult_factor).reshape(rows, cols)

    return result_img


# read the image
img = cv.imread("./img/boy.jpg")
  1. Write a program to enhance the image in spatial domain using histogram equalization method
In [9]:
import cv2 as cv
import matplotlib.pyplot as plt

# read the image
img = cv.imread("./img/apple.png", 0)

equalized_img = cv.equalizeHist(img)

# display the equalized image
res = cv.vconcat([img, equalized_img])
cv.imshow("Original Image -> Equalized Image", res)
cv.waitKey(0)
cv.destroyAllWindows()
  1. Write a program to perform the following image enhancement methods
    • Brightness enhancement
    • Brightness suppression
    • Contrast manipulation
    • Gray level slicing without background
In [10]:
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline

# =============================
# handler functions
# =============================
def brightness_enhancement(color_img, beta=20):
    res_img = cv.convertScaleAbs(color_img, alpha=1, beta=beta)
    return res_img

def brightness_suppression(color_img, beta=-10):
    res_img = cv.convertScaleAbs(color_img, alpha=1, beta=beta)
    return res_img

def contrast_manipulation(color_img, alpha=1.5):
    res_img = cv.convertScaleAbs(color_img, alpha=alpha, beta=0)
    return res_img

def gray_level_slicing(color_img, T=150):
    gray_img = cv.cvtColor(color_img, cv.COLOR_BGR2GRAY)
    r, c= gray_img.shape
    L = 255  # Highest intensity value for 8 bit image
    img_thresh = np.zeros((r, c), dtype=np.uint8)
    for i in range(r):
        for j in range(c):
            if gray_img[i,j] < T:
                img_thresh[i,j] = 0
            else:
                img_thresh[i,j] = L
    return img_thresh

# ============================
# Driver code
img = cv.imread("./img/house.png")
rgb_img = cv.cvtColor(img, cv.COLOR_BGR2RGB)

# brightness enhancement
enhanced_img = cv.cvtColor(brightness_enhancement(img, 100), cv.COLOR_BGR2RGB)

# brightness suppression
suppressed_img = cv.cvtColor(brightness_suppression(img, -50), cv.COLOR_BGR2RGB)

# contrast manipulation
contrast_img = cv.cvtColor(contrast_manipulation(img, 2), cv.COLOR_BGR2RGB)

# gray level slicing w/o background
gls_res = gray_level_slicing(img)
gls_img = cv.cvtColor(gls_res, cv.COLOR_GRAY2RGB)


results = [rgb_img, enhanced_img, suppressed_img, contrast_img, gls_img]
results_title = ["Original Image", "Brightness Enhanced", "Brightness Suppressed", "Contrast Manipulated", "Gray Level Slicing w/o Background", None]

fig, ax = plt.subplots(3, 2, figsize=(8,8))
fig.suptitle("Spatial Domain Enhancements", fontsize=16, fontweight="bold")
k = 0
for i in range(3):
    for j in range(2):
        if i == 2 and j == 1:
            ax[i,j].axis("off")
            break
        ax[i,j].axis("off")
        ax[i,j].imshow(results[k])
        ax[i,j].set_title(results_title[k])
        k += 1

plt.show()
No description has been provided for this image
  1. Write a program to average two images together into a single image. Display the new image.
In [11]:
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt

# reading both the images
img1 = cv.imread("./img/world_map_color.jpg")
img2 = cv.imread("./img/world_map_line.jpg")

rgb_img1 = cv.cvtColor(img1, cv.COLOR_BGR2RGB)
rgb_img2 = cv.cvtColor(img2, cv.COLOR_BGR2RGB)

# resizing one of the image 
# to have same image size
img2 = cv.resize(img2, (640, 425))

# calculating the weight of the images
alpha = 1.0/2        # image1 weight
beta = 1.0 - alpha  # image2 weight
# calculate average of the images
avg_img = cv.cvtColor(cv.addWeighted(img1, alpha, img2, beta, 0.0), cv.COLOR_BGR2RGB)

plt.figure(figsize=(8,8))
plt.subplot(2,2,1)
plt.imshow(rgb_img1)
plt.title("Original Image 1", fontsize=16, fontweight="bold")
plt.axis("off")

plt.subplot(2,2,2)
plt.imshow(rgb_img2)
plt.title("Original Image 2", fontsize=16, fontweight="bold")
plt.axis("off")

plt.subplot(2,2,(3,4))
plt.imshow(avg_img)
plt.title("Average Image", fontsize=16, fontweight="bold")
plt.axis("off")

plt.show()
No description has been provided for this image
  1. Write a program to compare two images using image subtraction
In [12]:
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt

def img_error(img1, img2):
    h, w = img1.shape
    diff = cv.subtract(img1, img2)
    err = np.sum(diff**2)
    mse = err/(float(h*w))     # mean squared error
    msre = np.sqrt(mse)        # mean square root error
    return mse, diff

img1 = cv.imread('./img/orange_bird.jpg')
img2 = cv.imread('./img/parrot.png')

img1 = cv.resize(img1, (453, 340))

rgb_img1 = cv.cvtColor(img1, cv.COLOR_BGR2RGB)
rgb_img2 = cv.cvtColor(img2, cv.COLOR_BGR2RGB)

img1 = cv.cvtColor(img1, cv.COLOR_BGR2GRAY)
img2 = cv.cvtColor(img2, cv.COLOR_BGR2GRAY)

match_error12, diff12 = img_error(img1, img2)

print("Image matching Error between image 1 and image 2:",match_error12)

plt.figure(figsize=(7,7))
plt.suptitle("Image Comparison using Image Subtraction", fontsize=16, fontweight="bold")

plt.subplot(2, 2, 1)
plt.imshow(rgb_img1)
plt.title("Image 1", fontweight="bold")
plt.axis('off')

plt.subplot(2, 2, 2)
plt.imshow(rgb_img2)
plt.title("Image 2", fontweight="bold")
plt.axis('off')

plt.subplot(2, 2, (3, 4))
plt.imshow(cv.cvtColor(diff12, cv.COLOR_GRAY2RGB))
plt.title("Image Difference", fontweight="bold")
plt.axis('off')


plt.show()
# o/p - Image matching Error between image 1 and image 2: 5.3371853044496484
# Image matching Error between image 1 and image 2: 10.701824438384625
Image matching Error between image 1 and image 2: 10.701824438384625
No description has been provided for this image
  1. Write a program to perform the following image arithmetic.
    • Image addition
    • Image subtraction
    • Image multiplication
    • Image Division
In [13]:
import cv2 as cv
import matplotlib.pyplot as plt

# reading images
img1 = cv.imread("./img/flowers.jpg")
img2 = cv.imread("./img/boy.jpg")

h, c, _ = img1.shape
img2 = cv.resize(img2, (c, h))

# image addtion
add_img = cv.addWeighted(img1, 0.7, img2, 0.4, 0.0)

# image subtraction
sub_img = cv.subtract(img1, img2)

# image multiplication
mult_img = cv.multiply(img1, img2)

# image division
div_img = cv.divide(img1, img2)

# resultant image set
result_img = [img1, img2, add_img, sub_img, mult_img, div_img]
result_titles = ["Image 1", "Image 2", "Additon", "Subtraction", "Multiplication", "Division"]

fig, ax = plt.subplots(3, 2, figsize=(10,10))
fig.suptitle("Image Arithmetic Operations", fontsize=16, fontweight="bold")
k = 0
for i in range(3):
    for j in range(2):
        ax[i,j].axis("off")
        ax[i,j].imshow(cv.cvtColor(result_img[k], cv.COLOR_BGR2RGB))
        ax[i,j].set_title(result_titles[k])
        k += 1

plt.show()
No description has been provided for this image
  1. Write a program to add various types of noise (salt and pepper noise, Gaussian noise) to an image
In [14]:
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt

def add_gaussian_noise(image, mean=0, std=25):
    row, col, ch = image.shape
    gauss = np.random.normal(mean, std, (row, col, ch))
    noisy_img = image + gauss.astype(np.uint8)
    return noisy_img

def add_salt_and_pepper_noise(image, noise_ratio=0.02):
    noisy_img = image.copy()
    h, w, ch = noisy_img.shape
    noisy_pixels = int(h * w * noise_ratio)

    for _ in range(noisy_pixels):
        row, col = np.random.randint(0, h), np.random.randint(0, w)
        if np.random.rand() < 0.5:
            noisy_img[row, col] = [0, 0, 0]
        else:
            noisy_img[row, col] = [255, 255, 255]

    return noisy_img

    
# read an image
img = cv.imread("./img/lena.jpg")

# adding noises
gauss_noise_img = add_gaussian_noise(img)
sp_noise_img = add_salt_and_pepper_noise(img)

# display image
plt.figure(figsize=(10,10))
plt.subplot(1, 3, 1)
plt.imshow(cv.cvtColor(img, cv.COLOR_BGR2RGB))
plt.title("Original")
plt.axis("off")

plt.subplot(1, 3, 2)
plt.imshow(cv.cvtColor(gauss_noise_img, cv.COLOR_BGR2RGB))
plt.title("Gaussian Noise")
plt.axis("off")

plt.subplot(1, 3, 3)
plt.imshow(cv.cvtColor(sp_noise_img, cv.COLOR_BGR2RGB))
plt.title("Salt & Pepper Noise")
plt.axis("off")

plt.show()
No description has been provided for this image
  1. Write a program to enhance an image using mean filtering, weighted average filtering, median filtering, max/min filtering
In [15]:
import cv2 as cv
import matplotlib.pyplot as plt

# read the image
img = cv.imread("./img/logo.png")

# Apply mean filtering with a kernel of 5x5
meanFiltered_img = cv.blur(img, (5,5))

# Apply median filtering with a kernel of 5x5
medianFiltered_img = cv.medianBlur(img, 5)

# display the image
plt.figure(figsize=(8,8))
plt.subplot(2,3,1)
plt.imshow(cv.cvtColor(img, cv.COLOR_BGR2RGB))
plt.title("Original")
plt.axis("off")

plt.subplot(2,3,2)
plt.imshow(cv.cvtColor(meanFiltered_img, cv.COLOR_BGR2RGB))
plt.title("Mean Filter")
plt.axis("off")

plt.subplot(2,3,3)
plt.imshow(cv.cvtColor(medianFiltered_img, cv.COLOR_BGR2RGB))
plt.title("Median Filter")
plt.axis("off")

plt.show()
No description has been provided for this image
In [16]:
import cv2 as cv
import matplotlib.pyplot as plt

# read the image
img = cv.imread("./img/noisyParrot.png", 0)

# defining kernel for the weighted average filter
wt_kernel = np.array([[1,2,1], [2,4,2], [1,2,1]])/16

# Apply the weighted average filter
filtered_img = cv.filter2D(src=img, ddepth=-1, kernel=wt_kernel)

plt.figure(figsize=(8,8))
plt.subplot(1, 2, 1)
plt.imshow(img, cmap='gray')
plt.title("Original")
plt.axis("off")

plt.subplot(1, 2, 2)
plt.imshow(filtered_img, cmap='gray')
plt.title("Weighted Average")
plt.axis("off")

plt.show()
No description has been provided for this image
In [17]:
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt

img = cv.imread("./img/noisyParrot.png")
rgb_img = cv.cvtColor(img, cv.COLOR_BGR2RGB)

# creating kernel for the min-max filter of 5x5 
kernel = np.ones((5,5), dtype=np.uint8) / 25

# Applying the min-max filter
filtered_img = cv.filter2D(src=rgb_img, ddepth=-1, kernel=kernel)

plt.figure(figsize=(8,8))
plt.title("Min Max Filter")
res_img = np.hstack((rgb_img, filtered_img))
plt.imshow(res_img)
plt.axis("off")
plt.show()
No description has been provided for this image
  1. Write a program to segment an image using thresholding technique
In [18]:
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt

# read the image
img = cv.imread("./img/blurred.jpg", cv.IMREAD_GRAYSCALE)

# apply thresholding with 
# threshold value 127
ret, thresh = cv.threshold(img, 127, 255, cv.THRESH_BINARY)

# display the segmented image
plt.title("Before and After Segmentation")
res_img = np.hstack((img, thresh))
plt.imshow(res_img, cmap="gray")
plt.axis("off")
plt.show()
No description has been provided for this image
  1. Write a progra to segment an image based on
    • Region growing
    • Region splitting
    • Region merging
In [19]:
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt

# read the image
img = cv.imread("./img/birds.jpg")
rgb_img = cv.cvtColor(img, cv.COLOR_BGR2RGB)
img = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
h, c = img.shape

# creating a mask with zeros of the same size as the input image
mask = np.zeros((h+2, c+2), dtype=np.uint8)

# define seed point for the region growing
seed_point = (100, 250)

# specify the color of the fill region (eg. black)
new_color = (255, 255, 255)

# using floodFill() function to fill the connected component
retval, res_img, _, _ = cv.floodFill(img, mask, seed_point, new_color, loDiff=(50,50,50), flags=cv.FLOODFILL_FIXED_RANGE)

plt.figure(figsize=(10,10))
plt.subplot(1, 2, 1)
plt.imshow(rgb_img)
plt.title("Original")
plt.axis("off")

plt.subplot(1, 2, 2)
plt.imshow(res_img, cmap='gray')
plt.title("Region Growing Segmentation")
plt.axis("off")
plt.show()
No description has been provided for this image
In [20]:
# Region splitting
import cv2 as cv
import matplotlib.pyplot as plt

# Load an image
img = cv.imread("./img/birds.jpg")
rgb_img = cv.cvtColor(img, cv.COLOR_BGR2RGB)

# Convert the image to the L*a*b* color space
lab_image = cv.cvtColor(img, cv.COLOR_BGR2Lab)

# Apply the mean shift filter
result = cv.pyrMeanShiftFiltering(lab_image, 21, 51)

# Convert the result back to BGR color space
segmented_img = cv.cvtColor(result, cv.COLOR_Lab2BGR)
res_img = cv.cvtColor(segmented_img, cv.COLOR_BGR2RGBA)

# Display the original and segmented images
plt.figure(figsize=(10,10))
plt.subplot(1, 2, 1)
plt.imshow(rgb_img)
plt.title("Original")
plt.axis("off")

plt.subplot(1, 2, 2)
plt.imshow(res_img)
plt.title("Region Splitting")
plt.axis("off")
plt.show()
No description has been provided for this image
In [ ]:
# region merging
import cv2
import numpy as np

# Load the image
image = cv2.imread("./img/birds.jpg")

# Convert the image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Perform image binarization using Otsu's method
_, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)

# Create the marker image and initialize it to 0 (black)
markers = np.zeros_like(binary)

# Specify the size of the connectivity component (region)
connectivity = 8

# Perform the region merging
cv2.watershed(image, markers)

# Use the markers to segment the image
markers = cv2.watershed(gray, markers)

# Create a new image that will store the final segmented regions
segmented_regions = np.zeros_like(image)

# Loop through each marker and add the corresponding region to the final segmented regions image
for i in np.unique(markers):
    if i == -1: # the region represents the background, which we'll leave as black (0)
        continue
    # add the region to the final segmented regions image
    segmented_regions[markers == i] = (0, 255, 0) # color the segmented regions with green

# Display the results
# cv2.imshow('Original Image', image)
# cv2.imshow('Segmented Regions', segmented_regions)

# cv2.waitKey(0)
# cv2.destroyAllWindows()
  1. Write a program to find the edge of a given image with the following operators
    • Difference operator
    • Robert operator
    • Prewitt operator
    • Sobel operator
In [24]:
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt

def apply_edge_operators(image):
    # Convert the image to grayscale
    gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)

    # Difference operator
    diff_operator = cv.subtract(gray, cv.erode(gray, None))

    # Robert operator
    robert_x = cv.filter2D(gray, -1, np.array([[1, 0], [0, -1]]))
    robert_y = cv.filter2D(gray, -1, np.array([[0, 1], [-1, 0]]))
    robert_operator = np.sqrt(robert_x**2 + robert_y**2).astype(np.uint8)

    # Prewitt operator
    prewitt_x = cv.filter2D(gray, -1, np.array([[-1, 0, 1], [-1, 0, 1], [-1, 0, 1]]))
    prewitt_y = cv.filter2D(gray, -1, np.array([[-1, -1, -1], [0, 0, 0], [1, 1, 1]]))
    prewitt_operator = np.sqrt(prewitt_x**2 + prewitt_y**2).astype(np.uint8)

    # Sobel operator
    sobel_x = cv.Sobel(gray, cv.CV_64F, 1, 0, ksize=3)
    sobel_y = cv.Sobel(gray, cv.CV_64F, 0, 1, ksize=3)
    sobel_operator = np.sqrt(sobel_x**2 + sobel_y**2).astype(np.uint8)

    return diff_operator, robert_operator, prewitt_operator, sobel_operator

image = cv.imread("./img/spidy.jpg")

# Apply edge operators
diff_op, robert_op, prewitt_op, sobel_op = apply_edge_operators(image)

# Display the results
plt.figure(figsize=(12, 8))

plt.subplot(2, 3, 1), plt.imshow(cv.cvtColor(image, cv.COLOR_BGR2RGB)), plt.title('Original')
plt.axis("off")
plt.subplot(2, 3, 2), plt.imshow(diff_op, cmap='gray'), plt.title('Difference Operator')
plt.axis("off")
plt.subplot(2, 3, 3), plt.imshow(robert_op, cmap='gray'), plt.title('Robert Operator')
plt.axis("off")
plt.subplot(2, 3, 4), plt.imshow(prewitt_op, cmap='gray'), plt.title('Prewitt Operator')
plt.axis("off")
plt.subplot(2, 3, 5), plt.imshow(sobel_op, cmap='gray'), plt.title('Sobel Operator')
plt.axis("off")
plt.show()
No description has been provided for this image
  1. Write a program to read two images 'lena.bin' and 'peppers.bin'. Define a new 256 x 256 image as J as follows: the left half of J i.e. the first 128 columns, should be equal to the left half of lena image and the right half of J, i.e. the 129th column through the 256th column should be equal to the right half of pepper image. Show J image.
In [25]:
import cv2 as cv
import numpy as np
import matplotlib.pyplot as plt

# read the images
lena = cv.imread("./img/world_map_color.jpg")
peppers = cv.imread("./img/world_map_line.jpg")

# resizing images
lena = cv.cvtColor(cv.resize(lena, (256, 256)), cv.COLOR_BGR2RGB)
peppers = cv.cvtColor(cv.resize(peppers, (256, 256)), cv.COLOR_BGR2RGB)

# create a new empty canvas 'J' of 256 x 256 size
J = np.zeros((256, 256, 3), dtype=np.uint8)

# set the left half of 'J' to the left half of 'lena'
J[:, :128] = lena[:, :128]

# set the right half of 'J' to the right half of 'peppers'
J[:, 128:] = peppers[:, 128:]

# display the image 'J'
plt.figure(figsize=(8, 6))

plt.subplot(2,2,1)
plt.imshow(lena)
plt.title("First Image")
plt.axis("off")
plt.tight_layout()

plt.subplot(2,2,2)
plt.imshow(peppers)
plt.title("Second Image")
plt.axis("off")
plt.tight_layout()

plt.subplot(2, 2, (3,4))
plt.imshow(J)
plt.title("Merged Image 'J'")
plt.axis("off")
plt.tight_layout()
plt.show()
No description has been provided for this image